package com.situ.crm.test;

import com.situ.crm.pojo.entity.User;
import org.junit.Test;

public class Demo {

    public static void main(String[] args) {
        String str1 = "a";
        String str2 = "b";
        String str3 = "c";
        String str = str1 + str2 + str3;
        String s = "a" + "b" + "c";

        StringBuilder builder = new StringBuilder("a");
        builder.append(str2);
        builder.append(str3);
        builder.append(str2).append(str3);
    }

    // 事物
    public void zhuanZhang() {
        System.out.println("Demo.test");
        // sql1   -1000
        // sql2   +1000
    }

    int money = 1000;
    // 锁加到方法上是把整个方法的代码锁住了
    public synchronized void quQian1() {
        // ......
        if (money >= 1000) {
            money -= 1000;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // ......
    }

    public void quQian2() {
        // ......
        //  如果觉得锁住整个方法锁住的代码太多了，可以只锁住需要锁住的那部分代码
        synchronized (this) {
            if (money >= 1000) {
                money -= 1000;
            }
        }
        // ......
    }

    @Test
    public void  test() {
        User user1 = new User();
        User user2 = new User();
        User user3 = new User();

        // Student student1 = new Student();
        // Student student2 = new Student();

        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        Singleton singleton3 = Singleton.getInstance();
        System.out.println(singleton1 == singleton2);
        System.out.println(singleton2 == singleton3);

        Singleton2 s1  = Singleton2.getInstance();
        Singleton2 s2  = Singleton2.getInstance();
        Singleton2 s3  = Singleton2.getInstance();
        System.out.println(s1 == s2);
        System.out.println(s2 == s3);
    }
}
